/**
 * 公共附件管理commonFile.js
 * 版本号： v1.0.0
 */
$(function () {
    //附件绑定功能名称
    bindCombo_funDist("funName_");
    //绑定模块名称
    bindCombo_modelDist("modelName_");
    //绑定创建人名称
    bindCombo_userDist("createId_");

    /**
     *附件列表
     */
    var lastChecked = null;
    $("#commonFile_dataList").datagrid({
        fit: true,
        fitColumns: true,
        method: "POST",
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        border: false,
        striped:true,
        pageSize: 10,
        pageList: [10, 30, 50, 100],
        url: CONTEXT_PATH + '/commonFile/commonFileList.do',
        idField: 'fileId',
        frozenColumns: [[
            {field: 'fileId', checkbox: true}
        ]],
        columns: [
            [
                {field: 'name', title: '文件名称', width: 220},
                {field: 'fkType', title: '业务类型', width: 150, fixed: true},
                {field: 'modelName', title: '模块名称', width: 80, align: 'center', fixed: true},
                {field: 'modelCode', title: '模块代码', width: 80, fixed: true},
                {field: 'funName', title: '功能名称', width: 100, align: 'center', fixed: true},
                {field: 'funCode', title: '功能代码', width: 120, fixed: true},
                {field: 'createdate', title: '上传时间', width: 135, fixed:true}
            ]
        ],
        onCheck:function(rowIndex, rowData){
            var row = $("#commonFile_dataList").datagrid("getChecked");
            if(row && rowIndex == lastChecked){
                $('#commonFile_dataList').datagrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            }else{
                lastChecked = rowIndex;
            }
        },
        onLoadSuccess: function (data) {
            if (data.rows.length > 0) {
                lastChecked = null;
                $("#commonFile_dataList").datagrid("selectRow", 0);
            }
        }
    });

    //定义可操作按钮
    $("#commonFile_menu").dialog({
        title: '',
        width: 'auto',
        height: 35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });

});

/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for(var j = 0; j < btnOptions.length; j++ ){
        if(false != btnOptions[j].value){
            falg = true;
            break;
        }
    }
    if(!falg){
        //不存在按钮
        toolbar.push({});
    }else{
        //存在按钮
        for(var i = 0; i < btnOptions.length; i++){
            if(false != btnOptions[i].value){
                //查询按钮
                if("searchBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            commonFile_loadSearch();
                        }
                    });
                }
                //删除按钮
                if("delBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            commonFile_del();
                        }
                    });
                }
                //图片浏览
                if("imgBrowseBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            imgBrowseFun();
                        }
                    });
                }
                //详情按钮
                if("viewBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            commonFile_view($(this).linkbutton("options").iconCls);
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

/**
 * 图片浏览
 */
function imgBrowseFun(){
    var imgDiv = $('#browseBtn');
    imgDiv.attr("src","");
    var checked = $("#commonFile_dataList").datagrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({ title: '操作提示', msg: "请选择要浏览的图片", timeout: 3000, showType: "slide" });
        return;
    };
    var row = $("#commonFile_dataList").datagrid("getSelected");
    var fix = row.fix;

    //判断文件格式为图片的才能浏览
    if(fix == 'jpg' || fix == 'png' || fix == 'bmp' || fix == 'gif' || fix == 'pcx' || fix == 'tif' || fix == 'tga'
        || fix == 'svg' || fix == 'psd' || fix == 'cdr' || fix == 'pcd' || fix == 'dxf' || fix == 'ufo' || fix == 'eps'
        || fix == 'raw' || fix == 'WMF' || fix == 'webp'){
        $.ajax({
            url: CONTEXT_PATH + "/commonFile/getFileUrlById.do?id=" + row.fileId,
            type: "get",
            success: function (data) {
                var url = data.message;
                if(url != "" && url != null){
                    imgDiv.attr('src',url);
                }else{
                    imgDiv.attr('src',"");
                    $.messager.show({ title:'提示', msg: '服务器端图片已不存在!', timeout:3000, showType:"slide" });
                    return;
                }
                imgDiv.trigger("custom");
            }
        });
    }else{
        $.messager.show({ title: '操作提示', msg: "浏览文件不是图片,请重新选择!", timeout: 3000, showType: "slide" });
        return;
    }
}

/**
 * 查看公共附件详情
 */
function commonFile_view(iconCls){
    var checked = $("#commonFile_dataList").datagrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({ title: '操作提示', msg: "请选择要查看的附件", timeout: 3000, showType: "slide" });
        return;
    };
    var row = $("#commonFile_dataList").datagrid("getSelected");
    //IFRAM打开窗口
    layoutSub(fjxq.id, fjxq.url+row.fileId, iconCls, fjxq.closable,fjxq.title);
}



/**
 * 删除公共附件配置
 */
function commonFile_del() {
    var checked = $("#commonFile_dataList").datagrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({ title: '操作提示', msg: "请选择删除的公共附件", timeout: 3000, showType: "slide" });
        return;
    };
    var row = $("#commonFile_dataList").datagrid("getSelected");
    $.messager.confirm('删除提示', '您确定要删除 ' + row.name + ' 吗？', function (r) {
        if (r) {
            $.ajax({
                url: CONTEXT_PATH + "/commonFile/delCommonFile.do?id=" + row.fileId,
                type: "get",
                success: function (data) {
                    messager(data);
                    $("#commonFile_dataList").datagrid("reload", {
                        modelCode: $("#modelName_").combobox("getValue"),
                        funCode: $("#funName_").combobox("getValue"),
                        name: $("#fileName_").textbox("getValue")
                    });
                }
            })
        }
    });
}

/**
 *  加载查询
 */
function commonFile_loadSearch() {
    refreshPageNumber("commonFile_dataList");
    $("#commonFile_dataList").datagrid("reload", {
        modelCode: $("#modelName_").combobox("getValue"),
        funCode: $("#funName_").combobox("getValue"),
        name: $("#fileName_").textbox("getValue")
    });
    $("#commonFile_dataList").datagrid("clearSelections");

}

/**
 * 文件模块函数bind事件
 * @param modelControl
 */
function bindCombo_modelDist(modelControl) {
    $.ajax({
        url: CONTEXT_PATH + "/commonFile/getModelList.do",
        type: "get",
        success: function (data) {
            var combo = [{'text': '--请选择--', 'value': ''}];
            for (var i = 0; i < data.length; i++) {
                combo.push({"text": data[i].modelName, "value": data[i].modelCode});
            }
            $("#" + modelControl).combobox("loadData", combo);
        }
    })
}

/**
 * 文件功能函数onChange事件
 * @param funControl
 */
function bindCombo_funDist(funControl) {
    $("#modelName_").combobox({
        onChange: function (n, o) {
            $("#funName_").combobox("setValue", ''); //清空
            clear:true;
            $.ajax({
                url: CONTEXT_PATH + "/commonFile/getFunList.do",
                data: {modelCode: n},
                type: "get",
                success: function (data) {
                    var funcombo = [{'text': '--请选择--', 'value': ''}];
                    for (var i = 0; i < data.length; i++) {
                        funcombo.push({"text": data[i].funName, "value": data[i].funCode});
                    }
                    $("#" + funControl).combobox("loadData", funcombo);
                }
            })
        }
    });
}

/**
 * 创建人列表
 * @param userControl
 */
function bindCombo_userDist(userControl) {
    $.ajax({
        url: CONTEXT_PATH + "/commonFile/getUserList.do",
        type: "get",
        success: function (data) {
            var combo = [{'text': '--请选择--', 'value': ''}];
            for (var i = 0; i < data.length; i++) {
                combo.push({"text": data[i].userName, "value": data[i].userId});
            }
            $("#" + userControl).combobox("loadData", combo);
        }
    })
}





